package cn.wwcyc.controller;

import cn.wwcyc.domain.*;
import cn.wwcyc.exception.WWExceptionEnum;
import cn.wwcyc.mapper.TjYjSsMapper;
import cn.wwcyc.mapper.TjYjXxMapper;
import cn.wwcyc.service.CjService;
import cn.wwcyc.service.TjDwService;
import cn.wwcyc.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/yjss")
public class YjSsController {
    @Autowired
    TjDwService tjDwService;
    @Autowired
    UserService userService;
    @Autowired
    TjYjXxMapper tjYjXxMapper;
    @Autowired
    TjYjSsMapper tjYjSsMapper;
    @Autowired
    CjService cjService;
    //此分页函数中的返回对象中含有单位
    @RequestMapping("/getlatestpageBydwgxjh")
    public R<IPage<TjYjSsVo>> getLatestPage(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam(required = false) Long dwid,
            @RequestParam(required = false) Long gx,
            @RequestParam(required = false) String jh,
            HttpServletRequest request
    ) {
        //计时
        long start = System.currentTimeMillis();
        System.out.println("--------------start:" + start);

        QueryWrapper<TjYjXx> qw = new QueryWrapper<>();
        if (!StringUtils.isBlank(jh)) {
            qw.like("jh", jh);
        }
        if (gx != null) {
            qw.eq("gx", gx);
        }
        User user = userService.getCurrentUser(request);
        if(dwid == null)
            dwid = user.getDw();

        TjDw tjDw = null;
        if (dwid != null) {
            tjDw = tjDwService.getDwTreeById(dwid);
        }
        List<TjDw> dwlist = new ArrayList<>();
        dwlist = tjDwService.DwTreeToList(dwlist, tjDw);
        List<Long> dwIdList = new ArrayList<>();
        for (TjDw tjDw1 : dwlist) {
            dwIdList.add(tjDw1.getId());
        }
        if (dwIdList.size() > 0) {
            qw.in("dw", dwIdList);
        }

        long count = tjYjXxMapper.selectCount(qw);

        IPage<TjYjSsVo> page = new Page<>(pagenum, pagesize);
        page.setTotal(count);
        page.setPages(page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1);
        if (pagenum > page.getPages()) {
            page.setCurrent(page.getPages());
        }
        if (pagenum < 1) {
            page.setCurrent(1);
        }
        List<TjYjSsVo> tjYjSsVoList = tjYjSsMapper.getLatestPageBydwgxjh(page.getCurrent(), page.getSize(), jh, dwIdList, gx);
        page.setRecords(tjYjSsVoList);
        return R.success(page);

//        List<TjHsyVo> list = tjHsyMapper.selectPagewithObj(page.getCurrent(), page.getSize(), dwIdList, jh);
//        List<TjHslVo> tjHslVoList = new ArrayList<>();
//
//        //使用partition  by 一次性查出每口井的最新数据
//        List<String> jhList = new ArrayList<>();
//        for(TjHsyVo tjHsyVo : list){
//            jhList.add(tjHsyVo.getJh());
//        }
//        List<TjSsHsl> TjSsHslList = tjSsHslMapper.getLatestByJhs(jhList);
//        for(TjHsyVo tjHsyVo : list){
//            TjSsHsl tjSsHsl = TjSsHslList.stream().filter(tjSsHsl1 -> tjSsHsl1.getJh().equals(tjHsyVo.getJh())).findFirst().orElse(null);
//            TjHslVo tjHslVo = new TjHslVo();
//            if(tjSsHsl != null){
//                tjHslVo.setJh(tjHsyVo.getJh());
//                tjHslVo.setHsy(tjHsyVo);
//                tjHslVo.setDw(tjHsyVo.getDwObj());
//                tjHslVo.setCapacity(tjSsHsl.getCapacity());
//                tjHslVo.setSshsl(tjSsHsl.getSshsl());
//                tjHslVo.setCjsj(tjSsHsl.getCjsj());
////                tjHslVoList.add(tjHslVo);
//            }else{
//                tjHslVo.setJh(tjHsyVo.getJh());
//                tjHslVo.setHsy(tjHsyVo);
//                tjHslVo.setDw(tjHsyVo.getDwObj());
//            }
//            tjHslVoList.add(tjHslVo);
//        }
//        page.setRecords(tjHslVoList);
//        return R.success(page);
    }

    @RequestMapping("/gethistorypageByjh")
    public R<IPage<TjYjSsVo>> getHistoryPage(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam String jh,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startdate,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date enddate
    ){

        if(startdate != null && enddate != null){
            if(startdate.after(enddate)){
                return R.error(WWExceptionEnum.DATE_ERROR);
            }
        }else{
            return R.error(WWExceptionEnum.DATE_ERROR);
        }
        IPage<TjYjSsVo> page = new Page<>(pagenum, pagesize);
        QueryWrapper<TjYjSs> qw = new QueryWrapper<>();
        qw.between("cjsj", startdate, enddate);
        qw.eq("jh", jh);
        qw.orderByDesc("cjsj");
        Long total = tjYjSsMapper.selectCount(qw);
        page.setTotal(total);
        page.setPages(page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1);
        if (pagenum > page.getPages()) {
            page.setCurrent(page.getPages());
        }
        if (pagenum < 1) {
            pagenum = 1;
            page.setCurrent(1);
        }
        List<TjYjSsVo> yjssVoList = tjYjSsMapper.selectPageYjHistory(pagenum, pagesize, jh, startdate, enddate);
        page.setRecords(yjssVoList);
        return R.success(page);
    }
    @RequestMapping("/gethistoryByjh")
    public R<List<TjYjSsVo>> getHistory(
            @RequestParam String jh,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startdate,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date enddate
    ){


        List<TjYjSsVo> yjssVoList = tjYjSsMapper.selectYjHistory(jh, startdate, enddate);

        return R.success(yjssVoList);
    }
    @RequestMapping("/stopcj")
    public R<String> stopcj(HttpServletRequest request) {
        cjService.stopCjJob();
        return R.success("已停止");
    }

    @RequestMapping("/startcj")
    public R<String> startcj(HttpServletRequest request) {
        cjService.startCjJob();
        return R.success();
    }
}
